package logic

import (
	"context"
	"shopping/model/mysql"
	"shopping/model/redis"

	"shopping/shoprpc/internal/svc"
	"shopping/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductLikeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductLikeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductLikeLogic {
	return &ProductLikeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductLikeLogic) ProductLike(in *shoprpc.ProductLikeRequest) (*shoprpc.ProductLikeResponse, error) {
	// todo: add your logic here and delete this line

	var product mysql.Product
	mysql.DB.Debug().Model(&mysql.Product{}).Where("id=?", in.ProductId).First(&product)
	redis.Client.ZIncrBy("productLike", 1, product.Name)

	return &shoprpc.ProductLikeResponse{ProductId: int64(product.ID)}, nil
}
